package com.anolesoft.amljmes.fourteenDispatch.controller;

import com.anolesoft.amljmes.fourteenDispatch.dto.MesPlanDispatchingDto;
import com.anolesoft.amljmes.fourteenDispatch.service.info.TaskViewService;
import com.anolesoft.common.core.constant.HttpStatus;
import com.anolesoft.common.core.domain.LoginUser;
import com.anolesoft.common.core.web.domain.AjaxResult;
import com.anolesoft.common.security.util.SecurityUtils;
import com.anolesoft.yr.demo.BaseQueryParamVO;
import com.anolesoft.yr.demo.PageResult;
import com.anolesoft.yr.util.Constants;
import lombok.*;
import lombok.experimental.SuperBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;


@RestController
@CrossOrigin
@RequestMapping("task")
public class TaskViewController {
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @SuperBuilder
    @ToString(callSuper = true)
    @EqualsAndHashCode(callSuper=true)
    public static class DemoVO extends BaseQueryParamVO implements Serializable {
        private String pacrNo;//制造记录条码
        private String jixing;//机型
        private String tuhao;//图号
        private Integer processstate;//是否完工
        private String caozuozhe;//操作者
        private String paigongren;// 派工人
        private String jiedanren;// 接单人
        private String factoryId;//厂代号
    }

    @Autowired
    TaskViewService taskViewService;

    //查询派工人
    @PostMapping(value="/queryPgr",produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult querySendWorkersInfo(DemoVO demoVO){
        LoginUser loginUser = SecurityUtils.getLoginUser();
        demoVO.setFactoryId(loginUser.getFacCode());

        List<MesPlanDispatchingDto> allList = taskViewService.querySendWorkersInfo(demoVO);
        System.out.println(">>>>>>"+allList.toString());
        int pn =  demoVO.getPageNo().intValue();
        int ps = demoVO.getPageSize().intValue();
        int endIndex = pn * ps > allList.size() ? allList.size() : pn*ps;
        List<MesPlanDispatchingDto> list = allList.subList( (pn-1) *ps, endIndex  );
        PageResult pageResult = new PageResult(list, demoVO.getPageSize(), demoVO.getPageNo(), allList.size());
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,pageResult);
    }

    //查询操作者
    @PostMapping(value="/queryCzz",produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult queryHandlersInfo(DemoVO demoVO){
        LoginUser loginUser = SecurityUtils.getLoginUser();
        demoVO.setFactoryId(loginUser.getFacCode());
        demoVO.setCaozuozhe(loginUser.getEmpUid());

        List<MesPlanDispatchingDto> allList = taskViewService.queryHandlersInfo(demoVO);
        System.out.println(">>>>>>"+allList.toString());
        int pn =  demoVO.getPageNo().intValue();
        int ps = demoVO.getPageSize().intValue();
        int endIndex = pn * ps > allList.size() ? allList.size() : pn*ps;
        List<MesPlanDispatchingDto> list = allList.subList( (pn-1) *ps, endIndex  );
        PageResult pageResult = new PageResult(list, demoVO.getPageSize(), demoVO.getPageNo(), allList.size());
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,pageResult);
    }

    //派工者查询 2022-08-23
    @PostMapping(value="/queryPgrForJiedan",produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult queryPgrForJiedan(DemoVO demoVO){
        LoginUser loginUser = SecurityUtils.getLoginUser();
        demoVO.setFactoryId(loginUser.getFacCode());

        List<MesPlanDispatchingDto> allList = taskViewService.queryPgrForJiedan(demoVO);
        System.out.println(">>>>>>"+allList.toString());
        int pn =  demoVO.getPageNo().intValue();
        int ps = demoVO.getPageSize().intValue();
        int endIndex = pn * ps > allList.size() ? allList.size() : pn*ps;
        List<MesPlanDispatchingDto> list = allList.subList( (pn-1) *ps, endIndex  );
        PageResult pageResult = new PageResult(list, demoVO.getPageSize(), demoVO.getPageNo(), allList.size());
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,pageResult);
    }

    //操作者查询 2022-08-23
    @PostMapping(value="/queryCzzForJiedan",produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult queryCzzForJiedan(DemoVO demoVO){
        LoginUser loginUser = SecurityUtils.getLoginUser();
        demoVO.setFactoryId(loginUser.getFacCode());
        demoVO.setCaozuozhe(loginUser.getEmpUid());

        List<MesPlanDispatchingDto> allList = taskViewService.queryCzzForJiedan(demoVO);
        System.out.println(">>>>>>"+allList.toString());
        int pn =  demoVO.getPageNo().intValue();
        int ps = demoVO.getPageSize().intValue();
        int endIndex = pn * ps > allList.size() ? allList.size() : pn*ps;
        List<MesPlanDispatchingDto> list = allList.subList( (pn-1) *ps, endIndex  );
        PageResult pageResult = new PageResult(list, demoVO.getPageSize(), demoVO.getPageNo(), allList.size());
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,pageResult);
    }
}
